package com.sunducation.co.bpw;

import lombok.extern.slf4j.Slf4j;

/**
 * @description: 浮点数需要注意的问题
 * @author:chenwenbiao
 * @createTime:2019/7/8 13:41
 * @Version：1.0
 **/
@Slf4j
public class FloatNumber {

    /**
     * @description: 浮点数据比较--直接比较
     * @author:chenwenbiao
     * @createTime:2019-07-08 13:43:15 13:43
     * @version：1.0
     * @return
     **/
    public static void floatCompare(float x, float y){
        float d = x - y;

        if(d > 0){
            log.info("结果是：x > y");
        }
        else if(d < 0){
            log.info("结果是：x < y");
        }
        else{
            log.info("结果是：x = y");
        }
    }


    /**
     * @description: 浮点数据比较--使用绝对值比较
     * @author:chenwenbiao
     * @createTime:2019-07-08 13:43:15 13:43
     * @version：1.0
     * @return
     **/
    public static void floatCompare2(float x, float y){
        float d = Math.abs(x - y);

        if(d > 0){
            log.info("结果是：x > y");
        }
        else if(d < 0){
            log.info("结果是：x < y");
        }
        else{
            log.info("结果是：x = y");
        }
    }


    public static void main(String[] argv){
        float x = 0.6f;
        float y = 0.600000001f;


        floatCompare2(x , y);
    }
}
